﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoGame_bird2._0
{
    public partial class FrmMain : Form
    {
        private Game game;

        public FrmMain()
        {
            InitializeComponent();
        }


        private void gameArea_Paint(object sender, PaintEventArgs e)
        {
            //游戏已经初始化了
            if(game!=null)
            {
                game.Draw(e.Graphics,this.gameArea.Size);
            }
        }

        private void gameArea_Load(object sender, EventArgs e)
        {
            //初始化
            game = new Game();          
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameover;

            
        }

        private void Game_gameover()
        {
            if (game.num==10)
            {
                //“游戏胜利”
                MessageBox.Show("You are the Winner！");
                //重置游戏
                game = new Game();
                game.gameChanged += Game_gameChanged;
                game.gameOver += Game_gameover;
                //重绘游戏区域
                this.gameArea.Invalidate();

            }
            else if (game.num==-1)
            {
                //“游戏结束”
                MessageBox.Show("Sorry，You are Failed！");
                //重置游戏
                game = new Game();               
                game.gameChanged += Game_gameChanged;
                game.gameOver += Game_gameover;
                //重绘游戏区域
                this.gameArea.Invalidate();

            }



        }

        private void Game_gameChanged()
        {
            if (game != null)
            {
                //重绘游戏区域
                this.gameArea.Invalidate();
                
            }
            
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (game!=null)
            {
                //传递按键给game
                game.KeyDown(e.KeyCode.ToString());
                //重绘游戏区域
                this.gameArea.Invalidate();
                this.labelscore.Text ="当前得分："+ game.Score.ToString();
            }
        }


        private void panelRight_Paint(object sender, PaintEventArgs e)
        {

        }


        //“开始游戏”的按键
        private void btnStartGame_Click(object sender, EventArgs e)
        {            
            
            //重置游戏
            game = new Game();
            this.labelscore.Text = "当前得分：" + game.Score.ToString();
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameover;

            //重绘游戏区域
            this.gameArea.Invalidate();

            //封面按键图隐藏
            this.pictureBoxBack.Hide();
            //游戏启动
            game.Isgamestart = true;

        }

        //封面图的picturebox
        private void pictureBoxBack_Click(object sender, EventArgs e)
        {

        }

        //“关于我”的按键
        private void btnAboutMe(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();

        }

        private void labelscore_Click(object sender, EventArgs e)
        {

        }

        //分数的时钟
        private void timerscore_Tick(object sender, EventArgs e)
        {
            this.labelscore.Text = "当前得分：" + game.Score.ToString();
            this.gameArea.Invalidate();

        }
    }
}
